import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { NewChangeInfoService } from './new-change-info.service';
import { TablePageComponent } from '../../modal/table/table-page/table-page.component';

@Component({
  selector: 'app-new-change-info',
  templateUrl: './new-change-info.component.html',
  styleUrls: ['./new-change-info.component.scss'],
  providers: [NewChangeInfoService]
})
export class NewChangeInfoComponent implements OnInit {

  // 最近15天变更列表
  @ViewChild("changeTablePage", { read: ViewContainerRef })
  changeTablePage: ViewContainerRef;
  tablePageComponent: ComponentRef<TablePageComponent>;
  public getBody: any = {
    pageIndex: 1,
    pageSize: 50
  }
  thi
  //全选按钮
  public isCheckedAll: any = false;

  //条目历史列表
  public listData: any = [];
  //克隆分页列表
  public cloneList: any = [];

  //分页配置
  public listPageInfo = {
    currentPageNum: 1,
    pageSize: 50,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  constructor(
    public newChangeInfoService: NewChangeInfoService,
    public resolver: ComponentFactoryResolver,
  ) { }
  // 条数分页
  sizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.changeInfo();
    }
  }
  // 列表分页
  pageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.changeInfo(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }
  ngOnInit() {
    // 初始化日期控件
    flatpickr.localize(zh);
    this.changeInfo();

  }
  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      // 更新为全选
      _.forEach(this.listData, item => {
        item.isChecked = true;
      });
    } else {
      // 更新为不全选
      _.forEach(this.listData, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.listData, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.listData.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  //查看最近15天变更
  changeInfo(pageNum?, pageSize?) {
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.newChangeInfoService.changeInfo(this.getBody).subscribe(data => {
      if (data) {
        this.listData = data.list;
        this.cloneList = _.cloneDeep(data.list);
        this.controlDom();
        console.log(this.cloneList);
        this.showTablePage(this.listData);
        this.listPageInfo.currentPageNum = data.pageNum;
        this.listPageInfo.totalPages = data.pages;
        this.listPageInfo.total = data.total;
        this.listPageInfo.startRow = data.startRow;
        this.listPageInfo.endRow = data.endRow;
        console.log(data)
      }
    })
  }
  //导出-查看条目历史
  exportExcel() {
    let that = this;
    // console.log("===============");
    // let checkedItem = [];
    // _.forEach(this.listData, item => {
    //   if (item.isChecked) {
    //     checkedItem.push(item.nQdId);
    //   }
    // })
    // nQdIdList=checkedItem;
    // let converItem: any = checkedItem.join(',');
    // let getBody = {
    //   nNdId: this.nNdId,
    //   sOprDateStart: this.getBody.sOprDateStart,
    //   sOprDateEnd: this.getBody.sOprDateEnd
    // }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    // getBody.sFullName = encodeURI(getBody.sFullName)
    iframe.attr('src', environment.server + '/pms/v1/riskmanage/data/export/lastestChanged?token=' + sessionStorage.getItem('token'));
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  //根据编号是否相同操控dom
  controlDom() {
    let that = this;
    if (this.listData != null && this.listData.length > 0) {
      for (let i = 0; i < that.listData.length - 1; i++) {
        if (that.listData[i].nNjId == that.listData[i + 1].nNjId) {
          _.forIn(that.listData[i], (item1, key1) => {
            let index1 = i + 1;
            let index2 = i + 2;
            let id1 = "#route-table table tbody tr:nth-child(" + index2 + ") td div[id=" + key1 + "]";
            if (item1 == that.listData[i + 1][key1]) {

              // window['$'](id1).removeClass('font-color');
            } else {
              that.cloneList[i + 1][key1] = 'red';
              // window['$'](id1).addClass('font-color');
            }
          })
        }
      }
    }
  }
  //查看最近15天变更列表
  showTablePage(item) {
    // 判断组件是否已创建，如果创建先注销
    if (this.tablePageComponent && this.tablePageComponent.instance) {
      this.tablePageComponent.destroy();
    }

    // 动态创建组件的实例
    const tablePageComponent = this.resolver.resolveComponentFactory(TablePageComponent);
    this.tablePageComponent = this.changeTablePage.createComponent(tablePageComponent);
    this.tablePageComponent.instance.listData = item;
    this.tablePageComponent.instance.listType = '3';
    this.tablePageComponent.instance.setColor = this.cloneList;
  }

}
